package controller;

import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Map;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/5/20 15:51
 * Version:1.0-SNAPSHOT
 * Description:
 */
@Controller
@RequestMapping("/param")
public class ParameterController {

    @RequestMapping("/f1")
    public void f1(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println("ParameterController.f1");
        ServletContext application = session.getServletContext();
        String path = application.getRealPath("");
        System.out.println("path:"+path);
    }

    // ServletContext不能直接作为Handler方法参数
    // 如果想要使用该对象
    // 必须通过其他方式进行获取
    @RequestMapping("/f2")
    public void f2(ServletContext application){
        System.out.println("ParameterController.f2");
    }

    @RequestMapping("/f3")
    public void f3(InputStream in, OutputStream out){
        System.out.println("ParameterController.f3");
    }

    @RequestMapping("/f4")
    public void f4(Reader reader, Writer writer){
        System.out.println("ParameterController.f4");
    }

    @RequestMapping("/f5")
    public void f5(PrintWriter out){
        out.print("<h1>Hello PrintWriter</h1>");
    }

    @RequestMapping("/f6")
    public void f6(InputStream in,Reader reader){
        System.out.println("ParameterController.f6");
    }

    @RequestMapping("/f7")
    public String f7(Model model, Map map, ModelMap modelMap){
        model.addAttribute("model","Hello Model");
        map.put("map","Hello Map");
        // ModelMap既可以使用Model的方法,也可以使用Map的方法
        modelMap.addAttribute("modelMap1","Hello ModelMap-Model");
        modelMap.put("modelMap2","Hello ModelMap-Map");
//        return "model";
        return "redirect:/showModel";
        // ?model=Hello+Model
        // &map=Hello+Map
        // &modelMap1=Hello+ModelMap-Model
        // &modelMap2=Hello+ModelMap-Map
        // 此时作用域中数据丢失
        // 但是丢失的数据将会以?传参的方式进行传递
        // 因此Model、Map、ModelMap与request作用域的主要区别
        // 当重定向之后,request作用域数据完全丢失
        // 但是Model、Map、ModelMap只是作用域中数据丢失,可以通过其他方法获取到丢失的数据
    }

    @RequestMapping("/f8")
    public String f8(HttpServletRequest request){
        request.setAttribute("model","Hello Model");
        return "redirect:/showModel";
    }

    @RequestMapping("/f9")
    public void f9(User user){
        System.out.println(user);
    }

    // 如果对象的属性是一个对象类型,Spring不会处理
    @RequestMapping("/f10")
    public void f10(Integer id,String username,String password,String phone,String address){
        User user = new User(id,username,password,phone,address);
        System.out.println(user);
    }

}
